#include<bits/stdc++.h>

using namespace std;

const int N = 2e5 + 10;
int a[N];

int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int t;
    scanf("%d",&t);
    while(t --)
    {
        int n;
        scanf("%d",&n);
        bool b = true;
        for(int i = 0;i < n;i ++)
        {
            scanf("%d",&a[i]);
            if(a[i] > 10)
                b = false;
        }
        if(n <= 15)
        {
            long long ans2 = 0;
            for(int s = 1;s < (1 << n);s ++)
            {
                long long sum = 0;
                for(int i = 0;i < n;i ++)
                {
                    for(int j = i - 1;j >= 0;j --)
                    {
                        if(((s >> i) & 1) == ((s >> j) & 1))
                        {
                            if(a[i] == a[j])
                                sum += a[i];
                            break;
                        }
                    }
                }
                ans2 = max(ans2,sum);
            }
            cout << ans2 << endl;
            continue;
        }
        if(b)
        {
            //cout << t << endl;
            long long ans2 = 0;
            for(int s = 0;s < (1 << 10);s ++)
            {
                long long sum = 0;
                int last1 = -1,last2 = -1;
                for(int i = 0;i < n;i ++)
                {
                    if((s >> (a[i] - 1)) & 1)
                    {
                        if(last1 != -1 && a[last1] == a[i])
                            sum += a[i];
                        last1 = i;
                    }
                    else if((s >> (a[i] - 1)) & 0)
                    {
                        if(last2 != -1 && a[last2] == a[i])
                            sum += a[i];
                        last2 = i;
                    }
                }
                //cout << s << ' ' << sum << endl;
                ans2 = max(ans2,sum);
            }
            cout << ans2 << endl;
            continue;
        }
    }
    return 0;
}
